package com.yanqu.road.server.manger.activity.twins;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.twins.TwinsDrawData;
import com.yanqu.road.entity.activity.twins.TwinsKungFuData;
import com.yanqu.road.entity.activity.twins.TwinsUserData;
import com.yanqu.road.entity.activity.twins.TwinsUserKungFuData;
import com.yanqu.road.entity.activity.twins.config.*;
import com.yanqu.road.entity.enums.activity.twins.eTwinsKungFuType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.TwinsBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.twins.TwinsModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class TwinsMgr extends TempMgr {

    private static RandomHelper randomHelper = new RandomHelper();

    private static TwinsConfig config;

    public static RandomHelper getRandomHelper() {
        return randomHelper;
    }

    public static void setRandomHelper(RandomHelper randomHelper) {
        TwinsMgr.randomHelper = randomHelper;
    }

    public static ActivityInfo getActivity() {
        if(config != null){
            return config.getActivityInfo();
        }
        return null;
    }

    public static int getActivityId() {
        if(config != null && config.getActivityInfo() != null){
            return config.getActivityInfo().getActivityId();
        }
        return 0;
    }

    public static TwinsConfig getConfig() {
        return config;
    }

    //获得打boss随机的两张卡
    public static TwinsKungFuData getSelfKungFu(TwinsUserData twinsUserData,int id){
        Map<Integer,TwinsUserKungFuData> missionKungFuMap = twinsUserData.getMissionKungFuMap();
        Map<Integer,TwinsUserKungFuData> myBagKungFuMap = twinsUserData.getGameBagKungFuMap();
        if(myBagKungFuMap.size() <= 0){
            for(TwinsUserKungFuData gaga : missionKungFuMap.values()){
                myBagKungFuMap.put(gaga.getKungFuId(),gaga);
            }
        }
        List<Integer> kungFuIdList = new ArrayList<>(myBagKungFuMap.keySet());
        int randomKungFuId = kungFuIdList.get(randomHelper.next(0,kungFuIdList.size()));
        TwinsUserKungFuData kungFuUserData = myBagKungFuMap.get(randomKungFuId);
        TwinsKungFuData kungFuData = new TwinsKungFuData();
        kungFuData.setId(id);
        kungFuData.setKungFuId(kungFuUserData.getKungFuId());
        kungFuData.setLevel(kungFuUserData.getLevel());
        kungFuData.setAnger(0);
        kungFuData.setNuqi(false);
        myBagKungFuMap.remove(randomKungFuId);
        twinsUserData.setGameBagKungFuMap(myBagKungFuMap);
        return kungFuData;
    }

    public static TwinsKungFuData getBossKungFu(int bossId,int id,int missionId){
        TwinsNpcConfig bossConfig = config.getNpcConfig(bossId);
        List<List<Integer>> oKungFuList = bossConfig.getOrdinaryKungfuList();
        TwinsKungFuData kungFuData = new TwinsKungFuData();
        kungFuData.setId(id);
        int randomIndex = randomHelper.next(0,oKungFuList.size());
        TwinsKungFuConfig kungFuConfig = config.getKungFuConfig(oKungFuList.get(randomIndex).get(0),oKungFuList.get(randomIndex).get(1));
        kungFuData.setLevel(oKungFuList.get(randomIndex).get(1));
        kungFuData.setKungFuId(oKungFuList.get(randomIndex).get(0));
        if(missionId == config.getBeginMissionId()){
            kungFuData.setAnger(1);
            kungFuData.setNuqi(false);
        }else{
            int minValue = kungFuConfig.getAngerList().get(0);
            int maxValue = kungFuConfig.getAngerList().get(1);
            if(oKungFuList.get(randomIndex).size() == 3){
                maxValue = maxValue - oKungFuList.get(randomIndex).get(2);
            }
            if(maxValue < minValue){
                maxValue = minValue;
            }
            kungFuData.setAnger(randomHelper.next(kungFuConfig.getAngerList().get(0),maxValue + 1));
            kungFuData.setNuqi(false);
        }
        return kungFuData;
    }

    //怒气卡 会有两张怒气卡一起集齐
    public static List<TwinsKungFuData> getBossAngerKungFuList(int bossId, AtomicInteger id,int missionId){
        List<TwinsKungFuData> resList = new ArrayList<>();
        TwinsNpcConfig bossConfig = config.getNpcConfig(bossId);
        List<List<Integer>> cKungFuList = bossConfig.getCoreKungfuList();
        for(List<Integer> kungFuInfoList : cKungFuList){
            TwinsKungFuData kungFuData = new TwinsKungFuData();
            kungFuData.setId(id.getAndIncrement());
            TwinsKungFuConfig kungFuConfig = config.getKungFuConfig(kungFuInfoList.get(0),kungFuInfoList.get(1));
            kungFuData.setLevel(kungFuInfoList.get(1));
            kungFuData.setKungFuId(kungFuInfoList.get(0));
            if(missionId == config.getBeginMissionId()){
                kungFuData.setAnger(1);
                kungFuData.setNuqi(true);
            }else{
                int minValue = kungFuConfig.getAngerList().get(0);
                int maxValue = kungFuConfig.getAngerList().get(1);
                if(kungFuInfoList.size() == 3){
                    maxValue = maxValue - kungFuInfoList.get(2);
                }
                if(maxValue < minValue){
                    maxValue = minValue;
                }
                kungFuData.setAnger(maxValue);
                kungFuData.setNuqi(true);
            }
            resList.add(kungFuData);
            if(resList.size() >= 2){
                break;
            }
        }
        return resList;
    }


    private static Map<Integer,List<Integer>> getQualityMap(Map<Integer, TwinsUserKungFuData> myKungFuMap){
        Map<Integer,List<Integer>> qualityMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, TwinsKungFuConfig>> kfMap = config.getKungFuConfigMap();
        //记录配置中有什么类型的武学
        List<Integer> kungfuTypeList = new ArrayList<>();
        //记录配置中有什么品质的武学
        List<Integer> qualityList = new ArrayList<>();
        for(Map<Integer, TwinsKungFuConfig> levelMap : kfMap.values()){
            if(!kungfuTypeList.contains(levelMap.get(1).getKungfuType())){
                kungfuTypeList.add(levelMap.get(1).getKungfuType());
            }
            if(!qualityList.contains(levelMap.get(1).getQuality())){
                qualityList.add(levelMap.get(1).getQuality());
            }
        }
        //初始武学所有类型都是1的都能抽中
        qualityMap.put(1,kungfuTypeList);
        for(TwinsUserKungFuData userKungFuData : myKungFuMap.values()){
            //现在等级可以是0了默认就1吧
            TwinsKungFuConfig kungFuConfig = config.getKungFuConfig(userKungFuData.getKungFuId(),1);
            if(kungFuConfig != null){
                if(!qualityMap.containsKey(kungFuConfig.getQuality())){
                    qualityMap.put(kungFuConfig.getQuality(),new ArrayList<>());
                }
                if(!qualityMap.get(kungFuConfig.getQuality()).contains(kungFuConfig.getKungfuType())){
                    qualityMap.get(kungFuConfig.getQuality()).add(kungFuConfig.getKungfuType());
                }
                if(qualityList.contains(kungFuConfig.getQuality() + 1)){
                    if(!qualityMap.containsKey(kungFuConfig.getQuality() + 1)){
                        qualityMap.put(kungFuConfig.getQuality() + 1,new ArrayList<>());
                    }
                    if(!qualityMap.get(kungFuConfig.getQuality() + 1).contains(kungFuConfig.getKungfuType())){
                        qualityMap.get(kungFuConfig.getQuality() + 1).add(kungFuConfig.getKungfuType());
                    }
                }
            }
        }
        return qualityMap;
    }

    //抽武功,抽奖规则看案子，太复杂了
    public static TwinsDrawData randomKungFu(Map<Integer, TwinsUserKungFuData> myKungFuMap){
        TwinsDrawData drawData = new TwinsDrawData();
        Map<Integer,List<Integer>> qualityMap = getQualityMap(myKungFuMap);
        Map<Integer, Map<Integer, TwinsKungFuConfig>> kungFuConfigMap = config.getKungFuConfigMap();
        List<Integer> weightList = config.getDrawWeightList();
        List<Integer> kungFuIdList = new ArrayList<>();
        List<Integer> kungFuWeightList = new ArrayList<>();
        for(int kungFuId : kungFuConfigMap.keySet()){
            TwinsKungFuConfig kfcConfig = kungFuConfigMap.get(kungFuId).get(1);
            if(qualityMap.containsKey(kfcConfig.getQuality())){
                if(qualityMap.get(kfcConfig.getQuality()).contains(kfcConfig.getKungfuType())){
                    if(kfcConfig.getQuality() - 1 < weightList.size()){
                        kungFuIdList.add(kungFuId);
                        kungFuWeightList.add(weightList.get(kfcConfig.getQuality() - 1));
                    }
                }
            }
        }
        List<Integer> expList = config.getDrawExpList();
        List<Integer> integralList = config.getDrawAddIntegralList();
        int kungFuId = 0;
        int expValue = 0;
        int integralValue = 0;
        int totalWeight = 0;
        for(int weight : kungFuWeightList){
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (int i = 0; i < kungFuWeightList.size();i++) {
            int value = kungFuWeightList.get(i);
            if (randomNum < value) {
                kungFuId = kungFuIdList.get(i);
                TwinsKungFuConfig kfcConfig = kungFuConfigMap.get(kungFuId).get(1);
                expValue = expList.get(kfcConfig.getQuality() - 1);
                integralValue = integralList.get(kfcConfig.getQuality() - 1);
                break;
            }
            randomNum -= value;
        }
        drawData.setKungFuId(kungFuId);
        drawData.setValue(expValue);
        drawData.setAddIntegralValue(integralValue);
        //抽奖变了又变
        /*Map<Integer,List<Integer>> qualityMap = getQualityMap(myKungFuMap);
        TwinsDrawData drawData = new TwinsDrawData();
        List<Integer> weightList = config.getDrawWeightList();
        List<Integer> expList = config.getDrawExpList();
        List<Integer> integralList = config.getDrawAddIntegralList();
        int quality = 0;
        int expValue = 0;
        int integralValue = 0;
        int totalWeight = 0;
        for(int qualityId : qualityMap.keySet()){
            totalWeight += weightList.get(qualityId - 1);
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (int i = 0; i < qualityMap.size();i++) {
            int value = weightList.get(i);
            if (randomNum < value) {
                quality = i + 1;
                expValue = expList.get(i);
                integralValue = integralList.get(i);
                break;
            }
            randomNum -= value;
        }
        drawData.setValue(expValue);
        drawData.setAddIntegralValue(integralValue);
        Map<Integer, Map<Integer, TwinsKungFuConfig>> kungFuConfigMap = config.getKungFuConfigMap();
        List<Integer> kungFuIdList = new ArrayList<>();
        for(int kungFuId : kungFuConfigMap.keySet()){
            TwinsKungFuConfig kfcConfig = kungFuConfigMap.get(kungFuId).get(1);
            if(kfcConfig.getQuality() == quality){
                if(qualityMap.get(quality).contains(kfcConfig.getKungfuType())){
                    kungFuIdList.add(kungFuId);
                }
            }
        }
        int rIndex = randomHelper.next(0,kungFuIdList.size());
        drawData.setKungFuId(kungFuIdList.get(rIndex));*/
        return drawData;
    }

    /**
     * 随机出来卡牌的斗志
     * @param kungFuId
     * @param level
     * @param combValue 组合武学增加的随机上限
     * @return
     */
    public static int randomAnger(int kungFuId,int level,int combValue,int missionId){
        TwinsKungFuConfig kungFuConfig = config.getKungFuConfig(kungFuId,level);
        if(kungFuConfig == null){
            return 0;
        }
        //第一个位置新手引导，随机出来的在最小值加1到最大值之间
        int guitAdd = 0;
        if(missionId == config.getBeginMissionId()){
            guitAdd = 1;
        }
        List<Integer> angerList = kungFuConfig.getAngerList();
        int anger = randomHelper.next(angerList.get(0) + guitAdd,angerList.get(1) + 1 + combValue);
        return anger;
    }

    public static int getNextMissionId(int missionId) {
        Map<Integer, TwinsMissionConfig> missionConfigMap = config.getMissionConfigMap();
        if(missionConfigMap.containsKey(missionId + 1)){
            return missionId + 1;
        }
        return 0;
    }

    public static int getNextBossId(int missionId, int bossId) {
        TwinsMissionConfig twinsMissionConfig = config.getMissionConfig(missionId);
        List<Integer> bossIdList = twinsMissionConfig.getBossIdList();
        if(!bossIdList.contains(bossId)){
            return 0;
        }
        if(bossIdList.indexOf(bossId) + 1 < bossIdList.size()){
            return bossIdList.get(bossIdList.indexOf(bossId) + 1);
        }
        return 0;
    }

    //随机一个功夫的技能刀法类型4的降低敌人斗志
    public static int randomKungFuFight(TwinsKungFuData kungFuData){
        TwinsKungFuConfig kungFuConfig = config.getKungFuConfig(kungFuData.getKungFuId(),kungFuData.getLevel());
        if(kungFuConfig.getKungfuType() == eTwinsKungFuType.Sabre.getValue()){
            return randomHelper.next(kungFuConfig.getSkillEffectList().get(3),kungFuConfig.getSkillEffectList().get(4));
        }
        return 0;
    }

    public static String randomMopUpReward(int missionId) {
        String randomReward = "";
        TwinsMissionConfig missionConfig = config.getMissionConfig(missionId);
        List<Integer> weightList = missionConfig.getSweepRandomRewardsList();
        List<String> rewardList = missionConfig.getSweepRandomRewardWeightList();
        if(weightList.size() == 0 || rewardList.size() == 0){
            return "";
        }
        int totalWeight = 0;
        for (int weight : weightList) {
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (int i = 0; i < weightList.size();i++) {
            int value = weightList.get(i);
            if (randomNum < value) {
                randomReward = rewardList.get(i);
                break;
            }
            randomNum -= value;
        }
        return randomReward;
    }

    //获取等级最高的加成
    public static int getCombAdd(List<Integer> unlockCombList,int combType){
        List<TwinsKungFuCombConfig> configList = new ArrayList<>();
        Map<Integer, TwinsKungFuCombConfig> configMap = config.getKungFuCombConfigMap();
        for(TwinsKungFuCombConfig combConfig : configMap.values()){
            if(combConfig.getType() == combType){
                configList.add(combConfig);
            }
        }
        int maxLevel = 0;
        int maxId = 0;
        for(TwinsKungFuCombConfig typeConfig : configList){
            if(unlockCombList.contains(typeConfig.getId())){
                if(maxLevel < typeConfig.getLevel()){
                    maxLevel = typeConfig.getLevel();
                    maxId = typeConfig.getId();
                }
            }
        }
        if(maxId != 0){
            return Integer.valueOf(configMap.get(maxId).getSkillEffect());
        }
        return 0;
    }

    //获取组合技能加成
    public static int getCombAdd(int combId){
        TwinsKungFuCombConfig combConfig = config.getKungFuCombConfig(combId);
        if(combConfig != null){
            return Integer.valueOf(combConfig.getSkillEffect());
        }
        return 0;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload Twins activity start");

        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Twins.getValue());
        if (openActivityInfoList.size() <= 0) {
            //用是不是有活动判断是不是
            if(config != null){
                config.setActivityInfo(null);
            }
            getLogger().info("no Twins activity in show time");
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);

        Map<Integer, Map<Integer, TwinsKungFuConfig>> tempKungFuConfigMap = TwinsBusiness.getKungFuConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsKungFuCombConfig> tempKungFuCombConfigMap = TwinsBusiness.getKungFuCombConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsMissionConfig> tempMissionConfigMap = TwinsBusiness.getMissionConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsNpcConfig> tempNpcConfigMap = TwinsBusiness.getNpcConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsLotteryConfig> tempLotteryConfigMap = TwinsBusiness.getLotteryConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsPvpConfig> tempPvpConfigMap = TwinsBusiness.getPvpConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, TwinsMapConfig> tempMapConfigMap = TwinsBusiness.getMapConfigMap(tmpActivityInfo.getActivityId());

        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        TwinsConfig tempConfig = new TwinsConfig();
        tempConfig.setActivityInfo(tmpActivityInfo);
        tempConfig.initConfig(activityConfigMap,tempKungFuConfigMap,tempKungFuCombConfigMap,tempMissionConfigMap,tempNpcConfigMap,tempLotteryConfigMap,tempPvpConfigMap,tempMapConfigMap);
        config = tempConfig;

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(TwinsModule.class).afterLogin();
        }
    }

    public static boolean canChangeUnion() {
        if (config == null) {
            return true;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        long now = System.currentTimeMillis();
        if (activityInfo != null) {
            if (ActivityMgr.activityInTime(activityInfo)) {
                if (UnionActivityGroupHelper.needGroup(activityInfo)){
                    return false;
                }
            }

        }
        return true;
    }


}
